package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;



import Dbcon.DBcon;
import dao.iface.DaoIface;
import domain.Things;

public class findDao implements DaoIface {

	@Override
	public boolean addInfo(String sql,Things thing) {
		Connection co=DBcon.getConnction();
		PreparedStatement ps=null;
		boolean b=false;
		try {
			ps=co.prepareStatement(sql);
			ps.setString(1, null);
			ps.setString(2,thing.getType());			
			ps.setString(3, thing.getTime());
			ps.setString(4, thing.getDescrible());
			ps.setString(5, thing.getStstus());
			ps.setString(6, thing.getPlace());
			ps.setString(7, thing.getSrc());
			ps.setString(8, thing.getName());
			ps.setString(9, thing.getPhonenumber());
			ps.setString(10, thing.getQqweixin());
			ps.setString(11, thing.getKey());
			System.out.println(thing.getType()+thing.getTime()+thing.getDescrible());
			
			int i=ps.executeUpdate();
			if(i<1){
				b=false;	
			}else{
				b=true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(b);
		return b;
	}

	


	@Override
	public List<Things> querryInfo(String sql,int begin, int end) {
		Connection co=DBcon.getConnction();
		 PreparedStatement ps = null;
		    ResultSet rs=null;
		    List<Things> list=new ArrayList<Things>();
		    
		    try {
				ps=co.prepareStatement(sql);
				ps.setInt(1, begin);
				ps.setInt(2, end);
				rs=ps.executeQuery();
				while(rs.next()) {
					String id = rs.getString("id");
					String date = rs.getString("date");
					String type = rs.getString("type");
					String describle = rs.getString("describle");
					String status = rs.getString("status");
					String place = rs.getString("place");
					Things thing = new Things(id, type, date, describle, status, place, "", "", "", "", "");
					list.add(thing);
				}
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return list;
	}




	@Override
	public int countquerry(String sql) {
		int b=0;
		Connection co=DBcon.getConnction();
		try {
			Statement st=co.createStatement();
			ResultSet rs=st.executeQuery(sql);
			while(rs.next()){
				b=rs.getInt(1);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return b;
	}




	@Override
	public Things seeInfo(String id, String sql) {
		Connection co=DBcon.getConnction();
		 PreparedStatement ps = null;
		    ResultSet rs=null;
		    Things thing=null;
		    try {
				ps=co.prepareStatement(sql);
				ps.setString(1, id);
				System.out.println(sql);
				rs=ps.executeQuery();
				rs.next();
				String date=rs.getString("date");
				String type=rs.getString("type");
				String describle=rs.getString("describle");
				String status=rs.getString("status");
				String place=rs.getString("place");
				String name=rs.getString("name");
				String phone=rs.getString("phone");
				String qq=rs.getString("qq");
				String src=rs.getString("src");
				System.out.println(date);
				 thing=new Things(id, type, date, describle,status, place, src, name, phone, qq, "");

			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return thing;
	}




	@Override
	public boolean deleteInfo(String id, String losekey,String sql) {
		Connection co=DBcon.getConnction();
		 PreparedStatement ps = null;
		    int i=0;
		    ResultSet rs=null;
		    boolean b=false;
		    try {
				ps=co.prepareStatement(sql);
				ps.setString(1, id);
				ps.setString(2, losekey);
				i=ps.executeUpdate();
				if(i==1){
					b=true;
					
				}else{
					b=false;
				}
				
				
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return b;
	}

}
